package task4;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class UserServer {
    public static void main(String[] args) {
        ServerSocket ss = null;
        Socket socket = null;
        ObjectInputStream ois = null;
        UserMessage um = null;
        ObjectOutputStream oos = null;
        try{
            ss = new ServerSocket(8888);
            System.out.println("服务器启动");
            socket = ss.accept();
            System.out.println("建立连接：" + socket.getInetAddress());
            ois = new ObjectInputStream(socket.getInputStream());
            um = (UserMessage)ois.readObject();
            // 判断接收到的对象信息
            if("admin".equalsIgnoreCase(um.getUser().getUserName()) && "123456".equals(um.getUser().getPassword())){
                um.setType("success");
            }else{
                um.setType("fail");
            }
            oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(um);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally{
            if(null == oos){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null == ois){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null == socket){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null == ss){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
